package logic

import (
	"context"

	// "mall/common/enumx"
	// "mall/common/sqrlx"
	// "mall/service/blog/model"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/types/blog"

	// sq "github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryListLogic {
	return &CategoryListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CategoryListLogic) CategoryList(in *blog.CategoryListRequest) (*blog.CategoryListResponse, error) {
	logx.Infof("PreloadFields : %+v ", in.PreloadField)

	// q := l.svcCtx.BlogTermModel.RowBuilder("t.`term_id`", "t")
	// tq := l.svcCtx.BlogTermTaxonomyModel.RowBuilder("", "tt")
	// aq := l.svcCtx.BlogTermModel.RowBuilder("", "")

	// q = q.Where(sq.Eq{"tt.`taxonomy`": string(enumx.TaxonomyCategory)})
	// if in.ObjectType > 0 {
	// 	q = q.Where(sq.Eq{"tr.`object_type`": in.ObjectType})
	// }
	// if in.Id > 0 {
	// 	q = q.Where(sq.Eq{"t.`term_id`": in.Id})
	// } else if in.IdIn != nil && len(in.IdIn) > 0 {
	// 	q = q.Where(sq.Eq{"t.`term_id`": sqrlx.UniqueInt64(in.IdIn)})
	// } else if in.IdNotIn != nil && len(in.IdNotIn) > 0 {
	// 	q = q.Where(sq.NotEq{"t.`term_id`": sqrlx.UniqueInt64(in.IdNotIn)})
	// }

	// ids, err := l.svcCtx.BlogTermModel.QueryIds(l.ctx, q, true, true)
	// if err != nil {
	// 	return &blog.CategoryListResponse{}, err
	// }

	// allIds := sqrlx.UniqueInt64(ids)
	// if len(allIds) == 0 {
	// 	return &blog.CategoryListResponse{}, nil
	// }

	// tq = tq.Where(sq.Eq{"t.`term_id`": allIds})
	// taxonomyList, err := l.svcCtx.BlogTermTaxonomyModel.QueryList(l.ctx, tq)
	// if err != nil {
	// 	return &blog.CategoryListResponse{}, err
	// }

	// idTaxonomyMap := make(map[int64]*model.BlogTermTaxonomy)
	// parentTermMap := make(map[int64]*model.BlogTerm)
	// for _, taxonomy := range taxonomyList {
	// 	idTaxonomyMap[taxonomy.TermId] = taxonomy
	// 	if taxonomy.ParentId > 0 {
	// 		allIds = append(allIds, taxonomy.ParentId)
	// 		parentTermMap[taxonomy.ParentId] = &model.BlogTerm{}
	// 	}
	// }	

	// aq = aq.Where(sq.Eq{"term_id": allIds})
	// TermList, err := l.svcCtx.BlogTermModel.QueryList(l.ctx, aq)
	// for _, term := range TermList {
	// 	if _, ok := parentTermMap[term.TermId]; ok { // 是父类
	// 		parentTermMap[term.TermId] = term
	// 	}
	// }

	var categories []*blog.CategoryDetailResponse
	// for _, term := range TermList {
	// 	if taxonomy, ok := idTaxonomyMap[term.TermId]; ok { // 是子类
	// 		var parentSlug string
	// 		if pid := taxonomy.ParentId; pid > 0 {
	// 			if parent, ok := parentTermMap[pid]; ok {
	// 				parentSlug = "/" + parent.Slug
	// 			}
	// 		}
	// 		categories = append(categories, &blog.CategoryDetailResponse{
	// 			Id:              term.TermId,
	// 			Count:           taxonomy.Count,
	// 			Description:     taxonomy.Description,
	// 			Link:            "category" + parentSlug + "/" + term.Slug,
	// 			Name:            term.Name,
	// 			ParentId:        taxonomy.ParentId,
	// 			Slug:            term.Slug,
	// 			TaxonomyName:    taxonomy.Taxonomy,
	// 			TermGroupID:     term.TermGroup,
	// 			TermTaxonomyID:  taxonomy.TermTaxonomyId,
	// 			URI:             "category" + parentSlug + "/" + term.Slug,
	// 		})
	// 	}
	// }

	return &blog.CategoryListResponse{
		Data: categories,
	}, nil
}
